package com.kuaimacode.kframework.code.template;

import com.kuaimacode.kframework.code.domain.Generate;
import com.kuaimacode.kframework.code.utils.CodeUtil;
import com.kuaimacode.kframework.code.utils.GenerateUtil;
import com.kuaimacode.kframework.code.utils.TemplateUtil;
import com.kuaimacode.kframework.util.ToolUtil;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.nio.file.FileAlreadyExistsException;
import java.util.List;

/**
 * @author Administrator
 * @date 2018/10/24
 */
public class ApiTemplate {

    /**
     * 生成需要导入的包
     */
    private static void genImport(Generate generate) {
        String tableEntity = generate.getBasic().getTableEntity();
        String genModule = generate.getBasic().getGenModule();
        String pacageName = TemplateUtil.getPath(generate) + ".mybatis.models."+ genModule + "." + tableEntity;
        //处理实体包名
        pacageName = pacageName.replaceAll("admin.*mybatis","mybatis");
        CodeUtil.importLine(pacageName);

        String servicePacageName = TemplateUtil.getPathNew(generate, "service") +"." + tableEntity + "Service";
        CodeUtil.importLine(servicePacageName.replace("admin", "api"));
        CodeUtil.importLine("com.kuaimacode.kframework.api.controller.BaseController");
        CodeUtil.importLine(JsonResult.class);
        CodeUtil.importLine(PageConstants.class);
        CodeUtil.importLine(Api.class);
        CodeUtil.importLine(ApiImplicitParam.class);
        CodeUtil.importLine(ApiImplicitParams.class);
        CodeUtil.importLine(ApiOperation.class);

        CodeUtil.importLine("org.springframework.web.bind.annotation.*");
        CodeUtil.importLine(HttpServletRequest.class);
        CodeUtil.importLine(Autowired.class);
        //CodeUtil.importLine("org.springframework.web.bind.annotation.*");
        CodeUtil.importLine(List.class);




    }

    /**
     * 生成类体
     */
    private static void getClazzBody(Generate generate) {
        // 构建数据
        String var = ToolUtil.lowerFirst(generate.getBasic().getTableEntity());
        String obj = generate.getBasic().getTableEntity();
        String title = generate.getBasic().getGenTitle();
        String module = generate.getBasic().getGenModule();
        String filePath = ApiTemplate.class.getResource("").getPath()
                + ApiTemplate.class.getSimpleName() + ".code";

        // 生成Class前部分
        String clazzTarget = TemplateUtil.getTemplate(filePath, "Class");
        clazzTarget = clazzTarget.replace("#{var}", var);
        clazzTarget = clazzTarget.replace("#{obj}", obj);
        clazzTarget = clazzTarget.replace("#{title}", title);
        CodeUtil.append(clazzTarget).append(CodeUtil.lineBreak);

        // 结束类
        CodeUtil.lineNo("}");
    }

    /**
     * 生成控制器模板
     */
    public static String generate(Generate generate) {
        CodeUtil.create();
        String packageName = TemplateUtil.getPackageNew(generate, "controller");
        packageName = packageName.replace("admin","api");
        CodeUtil.setPackageName(packageName);
        CodeUtil.addImportAll("org.springframework.web.bind.annotation");
        TemplateUtil.genAuthor(generate);
        ApiTemplate.genImport(generate);
        ApiTemplate.getClazzBody(generate);

        // 生成文件
        String filePath = GenerateUtil.getJavaFilePathNew(generate, "controller", "Controller");
        filePath = filePath.replace("admin","api");
        try {
            GenerateUtil.generateFile(filePath, CodeUtil.save());
        } catch (FileAlreadyExistsException e) {
            return GenerateUtil.fileExist(filePath);
        }
        return filePath;
    }
}
